package com.maybach.core.service.impl;

import com.maybach.common.enums.BaseConstants;
import com.maybach.core.ao.LoanOrderAO;
import com.maybach.dubbo.api.BizResult;
import com.maybach.dubbo.api.model.*;
import com.maybach.dubbo.api.query.LoanOrderQueryModel;
import com.maybach.dubbo.api.service.ILoanOrderService;
import com.maybach.util.AssertUtil;
import com.maybach.util.query.support.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * Created by houenxun on 16/2/25.
 */
@Component
public class LoanOrderServiceImpl implements ILoanOrderService {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    LoanOrderAO loanOrderAO;


    @Override
    public BizResult<LoanOrderCreateResultModel> createLoanOrder(CreateLoanOrderModel model) {
        AssertUtil.notNull(model, "model");
        BizResult<LoanOrderCreateResultModel> result = new BizResult<LoanOrderCreateResultModel>();
        try {
            if(!checkParam(model, result)){
                return result;
            }
            return loanOrderAO.addLoanOrder(model);
        } catch (Exception e) {
            logger.error("创建订单宝失败[model=" + model + "]", e);
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
            return result;
        }
    }

    @Override
    public BizResult<LoanOrderDetailModel> queryLoanOrderDetail(Long loanOrderId) {
        AssertUtil.notNull(loanOrderId, "loanOrderId");
        BizResult<LoanOrderDetailModel> result = new BizResult<LoanOrderDetailModel>();
        try {
            return loanOrderAO.queryOrderDetail(loanOrderId);

        } catch (Exception e) {
            logger.error("查询订单宝失败[loanOrderId=" + loanOrderId + "]", e);
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
        }
        return result;
    }

    @Override
    public BizResult<Void> closeOrder(Long orderId) {
        try {
            return loanOrderAO.closeLoanOrder(orderId);

        } catch (Exception e) {
            logger.error("关闭订单宝失败[model=" + orderId + "]", e);
            BizResult<Void> result = new BizResult<Void>();
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
            return result;
        }
    }

    @Override
    public BizResult<PageResult<LoanOrderModel>> queryLoanOrder(LoanOrderQueryModel query) {
        BizResult<PageResult<LoanOrderModel>> result = new BizResult<PageResult<LoanOrderModel>>();
        AssertUtil.notNull(query, "query");
        try {
            PageResult<LoanOrderModel> pageResult = loanOrderAO.queryLoanOrder(query);
            result.setResult(pageResult);

        } catch (Exception e) {
            logger.error("关闭订单宝失败[query=" + query + "]", e);
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
            return result;
        }
        return result;
    }

    @Override
    public BizResult<Void> submitOrderInfo(OrderInfoModel model) {

        try {
            return loanOrderAO.submitOrderInfo(model);

        } catch (Exception e) {
            logger.error("关闭订单宝失败[model=" + model + "]", e);
            BizResult<Void> result = new BizResult<Void>();
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
            return result;
        }
    }

    @Override
    public BizResult<Integer> countOrder(Long buyerId, Integer status) {
        AssertUtil.notNull(buyerId, "buyerId");
        try {
            return loanOrderAO.countOrder(buyerId, status);

        } catch (Exception e) {
            logger.error("查询订单宝失败[buyerId=" + buyerId+ ",status"+status + "]", e);
            BizResult<Integer> result = new BizResult<Integer>();
            result.setMsg(BaseConstants.getMessage(BaseConstants.ResultCode.EXCEPTION));
            return result;
        }
    }

    boolean checkParam(Object param,BizResult<LoanOrderCreateResultModel> result){
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Object>> violations = validator.validate(param);
        if (violations.size() > 0) {
            result.setMsg(violations.iterator().next().getMessage());
            return false;
        }
        return true;
    }
}
